/*
 * @lc app=leetcode.cn id=2 lang=javascript
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
  let node = new ListNode(0)
  let result = node
  let s = 0
  while(l1 || l2 || s) {
    const val1 = l1 ? (l1.val || 0) : 0
    const val2 = l2 ? (l2.val || 0) : 0
    let sum = val1 + val2 + s
    s = sum >= 10 ? 1 : 0
    sum = sum % 10
    if(l1) l1 = l1.next
    if(l2) l2 = l2.next
    node.next = new ListNode(sum)
    node = node.next
  }
  return result.next
};
// @lc code=end

